
var rowIdRet = null;


$(function(e){
	dataInit();
});

function dataInit(){
	eventInt();
	$('#messageTable').bootstrapTable({
		url: path+'272a5e1df86e4102bb4561f85143ea0c', method: 'post', striped: true, cache: false, pagination: true,
        contentType: "application/x-www-form-urlencoded", sidePagination: "server", pageSize: 10, uniqueId: "id", detailView: false,
        queryParams: function (params) {
        	return { limit: params.limit, offset: params.offset,searchParams:$("#searchParams").val()};
        },//传递参数（*）
        columns:[{field: 'Number', title: '序号', align: 'center', width: '50', formatter: function (value, row, index) {
        			return index+1;
        		}}, 
        		{field: 'infoName', title: '故障信息', align: 'center', width: '150', formatter: function (value, row, index) {
        			if(isNull(value)){
	            		value = '-';
	            	}
        			return '<a style="word-wrap:break-word;">'+value+'</a>';
	            }},
	            {field: 'faultName', title: '故障类型', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}
	            	return '<a style="word-wrap:break-word;">'+value+'</a>';
	            }},
	            {field: 'name', title: '创建人', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}
	            	return '<a style="word-wrap:break-word;">'+value+'</a>';
	            	
	            }},
	            {field: 'createTime', title: '创建时间', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}
	            	return '<a style="word-wrap:break-word;">'+value+'</a>';
	            	
	            }},
	            {field: 'operate', title: '操作', align: 'center', width: '210', events: {
	            	 'click .RoleOfA': function (e, value, row, index) {
	            		 	editFault(row.id);
		                },
		                'click .RoleOfB': function (e, value, row, index) {
		                	qiao.bs.confirm("确定要删除此项吗？",function(){
			                	deleteFault(row.id);
			                },function(){});
		                },
	            },
	            formatter: function (value, row, index) {
	            	return [
						'<button type="button" class="RoleOfA btn btn-default  btn-sm" style="margin-right:15px;">编辑</button>',
						'<button type="button" class="RoleOfB btn btn-default  btn-sm" style="margin-right:15px;">删除</button>',

	        	        ].join('');
	            }
        }],
        onLoadSuccess: function(data){  //加载成功时执行
        	if(data.returnCode!=0){
        		qiao.bs.msg({msg:data.returnMessage,type:'danger'});
        	}
        }
    });
	
	//修改
	$('#updatefaultForm').bootstrapValidator({
        fields: {
        	updateinfoName: {validators: {notEmpty: {message: '故障信息不能为空'}}},
        	updatefaultName: {validators: {notEmpty: {message: '故障类型不能为空'}}},
        },
        submitHandler: function (validator, form, submitButton) {
        	var params = {
					rowId:rowIdRet,
					infoName:$("#updateinfoName").val(),
					faultId:$("#updatefaultName").val(),
    				
    		};
    		AjaxPostUtil.request({url:path+"22b474c9cf464c0f927ef95aabc3a6d3",params:params,type:'json',callback:function(json){
    			if(json.returnCode==0){
    				qiao.bs.msg({msg:"保存成功",type:'success'});
    				$("#updateFaultMation").modal("hide");
    				$('#messageTable').bootstrapTable('refresh',{url:path+'272a5e1df86e4102bb4561f85143ea0c'});
    			}else{
    				qiao.bs.msg({msg:json.returnMessage,type:'danger'});
    			}
    		}});
    		return false;
        }
    });
	
	
	
	
	//增加
	$('#addfaultForm').bootstrapValidator({
	    fields: {
	    	infoName: {validators: {notEmpty: {message: '故障信息不能为空'}}},
	    	faultName: {validators: {notEmpty: {message: '故障类型不能为空'}}},
	    },
	    submitHandler: function (validator, form, submitButton) {
	    	var params = {
	    		infoName:$("#infoName").val(),
	    		faultId:$("#faultName").val(),
				
	    	};
	    	console.log(params);
	    	AjaxPostUtil.request({url:path+"cbc81b90b14148e2bdd5dcef9dc38ed9",params:params,type:'json',callback:function(json){
				if(json.returnCode==0){
					qiao.bs.msg({msg:"故障详情添加成功",type:'success'});
					$("#addFaultMation").modal("hide");
					$('#messageTable').bootstrapTable('refresh',{url:path+'272a5e1df86e4102bb4561f85143ea0c'});
				}else{
					qiao.bs.msg({msg:json.returnMessage,type:'danger'});
				}
			}});
			return false;
	    }
	});
	
	//删除
	function deleteFault(rowId){
		AjaxPostUtil.request({url:path+"c68c8f46eb8f4c59a4e84919625e2624",params:{rowId:rowId},type:'json',callback:function(json){
			if(json.returnCode==0){
				qiao.bs.msg({msg:"删除成功",type:'success'});
				$('#messageTable').bootstrapTable('refresh',{url:path+'272a5e1df86e4102bb4561f85143ea0c'});
			}else{
				qiao.bs.msg({msg:json.returnMessage,type:'danger'});
			}
			
			
			
		}});
	}
}

function editFault(rowId){
	
	AjaxPostUtil.request({url : path+"2ce082df1d024aa8a2ea45897da8b6fd", params : {rowId : rowId},type : 'json', callback : function(json){
		if(json.returnCode == 0){
			var source = $("#selectFaultInfo").html();
			var template = Handlebars.compile(source);
			$("#updatefaultName").html(template(json));
			AjaxPostUtil.request({url : path+"4c5edd4aa65a4fb986f2f487bca69029", params : {rowId : rowId}, type : 'json', callback : function(jsonMation){
				console.log(jsonMation);
				if(json.returnCode == 0){
					rowIdRet = jsonMation.bean.id;
					
					$("#updateinfoName").val(jsonMation.bean.infoName);
					
					$("#updatefaultName").attr(jsonMation.bean.faultName);
					$("#updatefaultName").val(jsonMation.bean.faultId);
					$("#updateFaultMation").modal("show");
				}else{
					qiao.bs.msg({msg:json.returnMessage,type:'danger'});
				}
			}});
		}else{
			qiao.bs.msg({msg : json.returnMessage, type : 'danger'});
		}
	}});
		
}


function eventInt(){
	//新增按钮时打开模态框
	$('body').on('click', '#addFault', function(e){
		AjaxPostUtil.request({url:path+"2ce082df1d024aa8a2ea45897da8b6fd",params:{},type:'json',callback:function(json){
			if(json.returnCode == 0){
				console.log(json);
				var source = $("#selectFaultInfo").html();
				var template = Handlebars.compile(source);
				$("#faultName").html(template(json));
				
				$("input").html("");
				$("#addFaultMation").modal("show");
			}else{
				qiao.bs.msg({msg : json.returnMessage, type : 'danger'});
			}
		}});
	});	
	
	//回车模糊搜索
	$('body').keydown(function() {
        if (event.keyCode == "13") {//keyCode=13是回车键
        	var params = {
        			limit : 10,
        			offset : 0,
        			searchParams : $("#searchParams").val()
        	}
        	AjaxPostUtil.request({url : path+"272a5e1df86e4102bb4561f85143ea0c", params : params, type : 'json', callback : function(json){
        		if(json.returnCode == 0){
        			console.log(json);
        			$('#messageTable').bootstrapTable('refresh',{url : path+'272a5e1df86e4102bb4561f85143ea0c'});
        		}else{
        			qiao.bs.msg({msg : '参数错误.', type : 'danger'});
        		}
        	}});
        }
	});
}

